package com.wy2.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wy2.entity.Role;
import com.wy2.mapper.RoleMapper;
import com.wy2.service.RoleService;
import com.wy2.util.FenyeUtil;
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleMapper rolemapper;
	/**
	 * 分页查询
	 */
	@Override
	public FenyeUtil<Role> getAll(FenyeUtil<Role> fenye) {
		Integer count = rolemapper.getCount(fenye);
		List<Role> data = rolemapper.getData(fenye);
		if(data.size()>0) {
			fenye.setCount(count);
			fenye.setData(data);
		}else {
			fenye.setCode(1);
			fenye.setMsg("无数据");
		}
		return fenye;
	}
    /**
     * 添加
     */
	@Override
	public Integer addRole(Role role) {
		// TODO Auto-generated method stub
		return rolemapper.addRole(role);
	}
    /**
     * 修改
     */
	@Override
	public Integer updRole(Role role) {
		// TODO Auto-generated method stub
		return rolemapper.updRole(role);
	}
    /**
     * 删除
     */
	@Override
	public Integer delRole(String rid) {
		// TODO Auto-generated method stub
		return rolemapper.delRole(rid);
	}
   /**
    * 查询全部
    */
	@Override
	public List<Role> getRole() {
		// TODO Auto-generated method stub
		return rolemapper.findAll();
	}
   /**
    * 查询全部角色
    */
	@Override
	public List<Role> findAllRole() {
		// TODO Auto-generated method stub
		return rolemapper.findAllRole();
	}

//	@Override
//	public Object getUsersRole(String uid) {
//		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
//		List<Role> data = rolemapper.getUserRole(uid);
//		for(Role roles:data) {
//			Map<String, Object> map = new HashMap<String, Object>();
//			map.put("value", roles.getRid());
//			result.add(map);
//		}
//		return result;
//	}
     /**
      * 添加
      */
	@Override
	public void saveUserole(String uid, String rid) {
		rolemapper.saveUserole(uid,rid);
		
	}
    /**
     * 删除
     */
	@Override
	public void delUserole(String uid, String rid) {
		rolemapper.delUserole(uid,rid);
		
	}
    /**
     * 根据用户id，查找角色
     */
	@Override
	public List<Role> getRoleByUid(String uid) {
		// TODO Auto-generated method stub
		return rolemapper.getRoleByUid(uid);
	}
     /**
      * 根据名字查询
      */
	@Override
	public List<Role> findRname(String rname) {
		
		return rolemapper.findRname(rname);
	}
   /**
    * 
    * 查询全部角色
    */
	@Override
	public List<Role> findAll() {
		// TODO Auto-generated method stub
		return rolemapper.findRoleAll();
	}
    /**
     * 查询用户所拥有的角色
     */
	@Override
	public Object getUserRole(String uid) {
		List<Map<String, Object>> result = new ArrayList<>();
		List<Role> data = rolemapper.getRoleByid(uid);
		for(Role role:data) {
			Map<String, Object> map = new HashMap<>();
			map.put("value", role.getRid());
			result.add(map);
		}
		return result;
	}


}
